<?php

/**
 * This is the model class for table "reg_user_account".
 *
 * The followings are the available columns in table 'reg_user_account':
 * @property string $user_id
 * @property string $password
 * @property string $user_type
 * @property string $status
 * @property string $name
 * @property string $surname
 * @property integer $prefix_id
 * @property string $created_by
 * @property string $created_date
 * @property string $modified_by
 * @property string $modified_date
 */
class UserAccount extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'reg_user_account';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('user_id, password, user_type, status, name, surname, prefix_id, created_by, created_date', 'required'),
			array('prefix_id', 'numerical', 'integerOnly'=>true),
			array('user_id, created_by, modified_by', 'length', 'max'=>20),
			array('password', 'length', 'max'=>100),
			array('user_type, status', 'length', 'max'=>50),
			array('name, surname', 'length', 'max'=>150),
			array('modified_date', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('user_id, password, user_type, status, name, surname, prefix_id, created_by, created_date, modified_by, modified_date', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function scopes() {
		return array(
				'teacher'=>array(
						'condition'=>'user_type="teacher"',
				),
		);
	}
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'prefix' => array(self::BELONGS_TO, 'RegPrefix', 'prefix_id'),
				'teacher' => array(self::BELONGS_TO, 'Teacher', 'user_id'),
				'regStudent'=>array(self::HAS_MANY, 'RegStudent', 'user_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'user_id' => 'User',
			'password' => 'Password',
			'user_type' => 'User Type',
			'status' => 'Status',
			'name' => 'ชื่อ',
			'surname' => 'นามสกลุล',
			'prefix_id' => 'คำนำหน้า',
			'created_by' => 'Created By',
			'created_date' => 'Created Date',
			'modified_by' => 'Modified By',
			'modified_date' => 'Modified Date',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($type=null)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('user_type',$this->user_type,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('surname',$this->surname,true);
		$criteria->compare('prefix_id',$this->prefix_id);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('created_date',$this->created_date,true);
		$criteria->compare('modified_by',$this->modified_by,true);
		$criteria->compare('modified_date',$this->modified_date,true);
		if ($type=='teacher'){
			$criteria->join = "LEFT JOIN mms_teacher ON t.user_id=mms_teacher.teacherID LEFT JOIN mms_committee ON mms_teacher.teacherID=mms_committee.teacherID";
			//$criteria->with=array('projectTeachers');
			//$criteria->with='teacher';
			//$criteria->with='teacher.commitees';
			$criteria->with=array('prefix','teacher.commitCount','teacher.projectCount');
			//$criteria->with='teacher.projectCount';
		}
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return UserAccount the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
